package com.gy.api.video.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.novelweb.tool.upload.local.pojo.UploadFileParam;
import com.github.pagehelper.PageInfo;
import com.gy.api.base.ApiIndex;
import com.gy.api.base.Constants;
import com.gy.api.base.R;
import com.gy.api.base.RestPage;
import com.gy.api.common.file.domain.vo.FileVO;
import com.gy.api.pc.examine.domain.vo.ExamineCompleteVO;
import com.gy.api.pc.movie.domain.vo.WcContentExaminePageListVO;
import com.gy.api.pc.movie.domain.vo.WcContentExamineSelectListDto;
import com.gy.api.video.domain.vo.FileAddVO;
import com.gy.api.video.domain.vo.FilePageSearchVO;
import com.gy.api.video.domain.vo.FilePageVO;
import com.gy.api.video.domain.vo.FileUpdateVO;
import com.gy.api.video.service.IFileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/video/")
@Api( tags = ApiIndex.FileUploadController)
@Slf4j
public class FileUploadController {
    @Autowired
    IFileUploadService service;

    @ApiOperation(value = "文件上传")
    @PostMapping(value = "/upload")
    @ResponseBody
    @SaCheckLogin
    public R<FileVO> upload(
            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "文件样品类型【1 报审影片样片  2 重点网络剧样片 3 微短剧样片】", required = true) @RequestParam(defaultValue = "1") String type
    ) {
        return service.upload(file,type);
    }

    @ApiOperation("文件列表")
    @GetMapping("/list")
    @ResponseBody
    public R<RestPage<FilePageVO>> acceptanceList(@ApiParam(value = "查询参数") @ModelAttribute FilePageSearchVO searchVO)  {
        searchVO.setUserId(StpUtil.getLoginIdAsString());
        if(StringUtils.isBlank(searchVO.getFileType())){
            searchVO.setFileType("1");
        }
        List<FilePageVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }


    @ApiOperation("更新文件名称")
    @PostMapping("updateName")
    @SaCheckLogin
    public R updateName(@RequestBody FileUpdateVO fileUpdateVO){
        service.updateName(fileUpdateVO);
        return new R();
    }




    @ApiOperation(value = "检查文件【分片上传】")
    @GetMapping(value = "/check_file")
    public R checkFileMd5(
            @ApiParam(value = "文件MD5", required = true) @RequestParam(defaultValue = "") String md5,
            @ApiParam(value = "文件名称", required = true) @RequestParam(defaultValue = "") String fileName
    ) throws Exception {
        return service.checkFileMd5(md5,fileName);
    }


    @ApiOperation(value = "文件上传完成【分片上传】")
    @PostMapping(value = "/success_file")
    public R successFile(
            @ApiParam(value = "参数") @Valid @RequestBody FileAddVO fileAddVO
    ) throws Exception {
        return service.successFile(fileAddVO.getMd5(),fileAddVO.getType(),fileAddVO.getFileName(),fileAddVO.getFileSize());
    }


    @ApiOperation(value = "文件分片上传【本地存储】")
    @PostMapping(value = "/upload_shard")
    @ResponseBody
    public R upload_shard(
            UploadFileParam uploadFileParam,
            HttpServletRequest request
    ) {
        return service.upload_shard(uploadFileParam,request);
    }






}
